package com.ds.infrastructure.hyperspace.container.internal;

import com.ds.infrastructure.hyperspace.common.Container;
import com.ds.infrastructure.hyperspace.common.config.ContainerConfig;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @author  Weishaoying
 */
@Slf4j
public abstract class AbstractContainer<T> implements Container<T> {

    public static final AtomicBoolean STATUS = new AtomicBoolean();

    @Override
    public void start(ContainerConfig containerConfig) {
        if (!STATUS.compareAndSet(false, true)) {
            log.warn("Start a container ignore, because a container was started!");
            return;
        }
        startInternal(containerConfig);
        log.info("Start hyperspace container successed!");
    }

    @Override
    public void shutdown() {
        if (!STATUS.compareAndSet(true, false)) {
            log.warn("Stop container ignore, container was stoped!");
            return;
        }
        stopInternal();
        log.info("Stop hyperspace container successed!");
    }

    /**
     * Initialize config, pipelines, endpoint, flow handlers etc...
     * @param containerConfig
     */
    protected abstract void startInternal(ContainerConfig containerConfig);

    /**
     * Release correlated resources
     */
    protected abstract void stopInternal();
}
